<?php
namespace app\mobile\controller;

use think\Controller;
use think\Session;
class Questions extends Controller
{
	public function index()
	{
		return $this->fetch('index');
	}
	
	public function question_list()
	{
		$result = array('err' => '1','content' => '','data' => array());
		$keyword = trim(input('get.keyword'));
		$pageSize = trim(input('get.pageSize'));
		if ($keyword == false || $keyword == null){
			$questions = db('questions')->field(['id','question_name','created'])->order('created','desc')->where('release','1')->paginate($pageSize);
		}else {
			$questions = db('questions')->field(['id','question_name','created'])->where('question_name','like','%'.$keyword.'%')->where('release','1')->order('created','desc')->paginate($pageSize);
		}
		$questions = $questions->toArray()['data'];
		foreach ($questions as $qk => &$qv){
			$qv['created'] = date('Y-m-d',$qv['created']);
		}
		$result['err'] = '0';
		$result['content'] = '获取数据成功！';
		$result['data'] = $questions;
		return $result;
	}
	
	public function question()
	{
		$question_id = trim(input('get.question_id'));
		$user_id = session('user_id','','mobile');
		$question_info = db('questions')->where('id',$question_id)->find();
		$questions = empty($question_info) ?array(): json_decode($question_info['questions'],true);
		$question_log = db('questionslog')->where(['user_id'=>$user_id,'questions_id'=>$question_id])->find();
		if (!empty($question_log) || $question_log != false){
			$user_questions = $question_log['questions'];
			$user_questions = json_decode($user_questions,true);
			foreach ($questions as $qk => &$qv){
				foreach ($user_questions as $uqk=>$uqv){
					if ($qv['question_id'] == $uqv['question_id']){
						$qv['iftrue'] = $uqv['iftrue'];
						switch ($qv['type']){
							case '1':
								$qv['user_answer'] = $uqv['answer'];
								break;
							case '2':
								$qv['user_answer'] = explode('&', $uqv['answer']);
								$qv['true_answer'] = explode('&', $qv['true_answer']);
								break;
							case '3':
								$qv['user_answer'] = $uqv['answer'];
								break;
						}
					}
				}
			}
			$this->assign('score',$question_log['score']);
			$this->assign('ifdone','done');
		}else{
			$this->assign('ifdone','never');
		}
		unset($question_info['questions']);
		$this->assign('user_id',session('user_id','','mobile'));
		$this->assign('role',session('role','','mobile'));
		$this->assign('question',$question_info);
		$this->assign('question_list',$questions);
		return $this->fetch('question');
		
	}
	
	public function save_user_answer()
	{
		$result = array('err' => '1','content' => '');
		if (Session::has('role','mobile') &&  Session::get('role','mobile') == 'admin'){
			$result['content'] = '您是管理员，不参与答题！';
			return $result;
		}
		$user_answer['user_id'] = Session::get('user_id','mobile');
		$user_answer['warehouse_id'] = Session::get('user_warehouse','mobile');
		$user_answer['questions_id'] = input('post.questions_id');
		$user_answer['questions'] = input('post.user_answer/a');
		$rules = [
				'user_id' => 'require|number',
				'questions_id' => 'require|number',
		];
		$message = [
				'user_id' => '用户参数错误！',
				'questions_id' => '答卷参数错误!'
		];
		$check_answer = $this->validate($user_answer, $rules,$message);
		if ($check_answer != 'true'){
			$result['content'] = $check_answer;
			return $result;
		}
		$questions_model = model('Questions');
		$save_user_answer = $questions_model->saveUserAnswer($user_answer);
		if ($save_user_answer == false){
			$result['content'] = '提交答卷失败！';
			return $result;
		}
		$result['err'] = '0';
		$result['content'] = '提交成功!';
		return $result;
	}
}